package com.web.dao.user;

import com.web.bean.user.User;
import com.web.utils.DBUtil;


import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @autor Chen
 * @create 2021--25-12:55
 * @desc
 */
public class UserDaoImpl implements UserDao {
     private Connection conn;
     private PreparedStatement pst;
     private ResultSet rs;

    public UserDaoImpl(Connection conn) {
        this.conn = conn;
    }


    @Override
    public User selectUserByPhone(String phone) throws Exception {
        conn = DBUtil.getInstance().getConnection();
        String sql = "select * from wn_user where phone = ?";
        User user = new User();
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1,phone);

            rs = pst.executeQuery();
            while (rs.next()){
                user = new User();
                user.setUid(rs.getString(phone));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (conn!=null){
                try {
                    conn.close();
                }catch (SQLException e){
                    e.printStackTrace();
                }
            }
        }
        return user;
    }

    @Override
    public List<User> selectUsers() throws Exception {
        List<User> users =new ArrayList<>();
        String sql = "select * from wn_user";
        pst = conn.prepareStatement(sql);
        rs = pst.executeQuery();
        while (rs.next()){
            User user = createUser(rs);
            users.add(user);
        }
        return users;
    }

    private User createUser(ResultSet rs) throws Exception{
        User user = null;
        user.setUid(rs.getString(1));
        user.setUname(rs.getString(2));
        user.setUpwd(rs.getString(3));
        user.setUdate(rs.getTimestamp(4));
        return user;
    }

    @Override
    public void addUser(User user) throws Exception {
        String sql ="insert into wn_user values(?,?,?,?,?,?,?)";
        pst = conn.prepareStatement(sql);
        pst.setString(1,user.getUid());
        pst.setString(2,user.getUname());
        pst.setString(3,user.getUpwd());
        pst.setString(4,user.getPhone());
        pst.setTimestamp(5,user.getUdate());
        pst.setString(6,user.getIsverify());
        pst.setInt(7,user.getUstatus());
        pst.executeUpdate();
    }

    @Override
    public void addWriter(User user) throws Exception {
        String sql ="insert into wn_user values (?,?,?)";
        pst = conn.prepareStatement(sql);
        pst.setString(1,user.getWname());
        pst.setString(2,user.getWpwd());
        pst.setString(3,user.getIdnum());
        user.setIsverify("2");
        pst.executeUpdate();
    }

    @Override
    public void updateUser(User user) throws Exception {
        String sql = "update wn_user set uname=?,upwd=?,udate=? where uid=?";
        pst = conn.prepareStatement(sql);
        pst.setString(1,user.getUname());
        pst.setString(2,user.getUpwd());
        pst.setTimestamp(3,user.getUdate());
        pst.setString(4,user.getUid());
    }

    @Override
    public void deleteUser(String uid) throws Exception {
        String sql = "delete from wn_user where uid = ?";
        pst = conn.prepareStatement(sql);
        pst.setString(1,uid);
        pst.executeUpdate();
    }

    @Override
    public void updateWriter(User user) throws Exception {
        String sql = "update wn_user set wname = ?,wpwd=? where uid = ?";
        pst = conn.prepareStatement(sql);
        pst.setString(1,user.getWname());
        pst.setString(2,user.getWpwd());
        pst.setString(3,user.getUid());
    }

    @Override
    public User selectWriterByPhone(String phone) throws Exception {
        User user = null;
        String sql = "select * from wn_user where phone = ?";
        pst = conn.prepareStatement(sql);
        pst.setString(1,phone);
        rs = pst.executeQuery();
        while (rs.next()){
            user = createUser(rs);
        }
        return user;
    }
}
